﻿Public Class generarSorteo

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Salir()
    End Sub

    Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter

    End Sub

    Private Sub btnBuscaCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCliente.Click
        LimpiaCliente()
    End Sub
    Private Sub TraeConsulta(ByVal ListaClientes As List(Of clsCliente))
        consultaCliente.ModoAcceso = "SORTEO"
        consultaCliente.grdClientes.DataSource = ListaClientes

        consultaCliente.grdClientes.Columns(0).DataPropertyName = "Cli_Id"
        consultaCliente.grdClientes.Columns(1).DataPropertyName = "Apellido"
        consultaCliente.grdClientes.Columns(2).DataPropertyName = "Nombre"
        consultaCliente.grdClientes.Columns(3).DataPropertyName = "Dni"
        consultaCliente.grdClientes.Columns(4).DataPropertyName = "Email"
        consultaCliente.grdClientes.Columns(5).DataPropertyName = "Cuit"
        consultaCliente.grdClientes.Columns(6).DataPropertyName = "Direccion"
        consultaCliente.grdClientes.Columns(7).DataPropertyName = "Altura"
        consultaCliente.grdClientes.Columns(8).DataPropertyName = "Depto"
        consultaCliente.grdClientes.Columns(9).DataPropertyName = "Tel"

        consultaCliente.grdClientes.Columns(10).DataPropertyName = "Tipo"
        consultaCliente.grdClientes.Columns(11).DataPropertyName = "Cod_suc"

        consultaCliente.grdClientes.Columns(0).HeaderText = "Codigo"
        consultaCliente.grdClientes.Columns(1).HeaderText = "Apellido"
        consultaCliente.grdClientes.Columns(2).HeaderText = "Nombre"
        consultaCliente.grdClientes.Columns(3).HeaderText = "DNI"
        consultaCliente.grdClientes.Columns(4).HeaderText = "Email"
        consultaCliente.grdClientes.Columns(5).HeaderText = "CUIT/CUIL"
        consultaCliente.grdClientes.Columns(6).HeaderText = "Direccion"
        consultaCliente.grdClientes.Columns(7).HeaderText = "Nro."
        consultaCliente.grdClientes.Columns(8).HeaderText = "Depto."
        consultaCliente.grdClientes.Columns(9).HeaderText = "Telefono"

        consultaCliente.grdClientes.Columns(10).HeaderText = "Tipo"
        consultaCliente.grdClientes.Columns(11).HeaderText = "Codigo Sucursal"

        consultaCliente.grdClientes.Columns(10).Visible = False
        consultaCliente.grdClientes.Columns(11).Visible = False

        consultaCliente.MdiParent = Me.MdiParent
        Me.Enabled = False
        consultaCliente.Show()
    End Sub
    Private Sub AsignaCampos(ByVal Cliente As clsCliente)
        txtCodClie.Text = Cliente.Cli_Id.ToString.Trim
        txtDni.Text = Cliente.Dni.Trim.ToUpper
        txtNomClie.Text = Cliente.Nombre.Trim.ToUpper
        txtApeClie.Text = Cliente.Apellido.Trim.ToUpper
        If Cliente.Tipo = "P" Then
            txtTipoClie.Text = "PARTICULAR"
        Else
            txtTipoClie.Text = "EMPRESA"
        End If

        txtCodClie.Enabled = False
        txtDni.Enabled = False
        txtNomClie.Enabled = False
        txtApeClie.Enabled = False
        txtTipoClie.Enabled = False
    End Sub
    Private Sub LimpiaCliente()
        txtCodClie.Text = ""
        txtDni.Text = ""
        txtNomClie.Text = ""
        txtApeClie.Text = ""
        txtTipoClie.Text = ""

        txtCodClie.Enabled = True
        txtDni.Enabled = True
        txtNomClie.Enabled = True
        txtApeClie.Enabled = True
        txtTipoClie.Enabled = True

    End Sub

    Private Sub btnLimpia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLimpia.Click
        Dim Cliente As New clsCliente()
        Dim ClienteDao As New daoCliente()
        Dim ListaCliente As New List(Of clsCliente)
        Dim ClienteBusiness As New businessCliente()

        ListaCliente = ClienteBusiness.TraeClientesAdjudicados()
        If ListaCliente.Count = 0 Then
            MessageBox.Show("No se encontraron clientes en condiciones de recibir Premios", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ElseIf ListaCliente.Count = 1 Then
            Cliente = ListaCliente.Item(0)
            AsignaCampos(Cliente)
        Else
            TraeConsulta(ListaCliente)
        End If
  
        Cliente.Dispose()
        ClienteDao.Dispose()
        ClienteBusiness.Dispose()

    End Sub

    Private Sub btnConsulta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsulta.Click
        Dim Producto As New clsCatalogo()
        Dim ProductoDao As New daoCatalogo()
        Dim ListaProducto As New List(Of clsCatalogo)
        Dim ProductoBusiness As New businessCatalogo()

        ListaProducto = ProductoBusiness.TraeArticulosAdjudicados()
        If ListaProducto.Count = 0 Then
            MessageBox.Show("No se encontraron Articulos para sorteos", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ElseIf ListaProducto.Count = 1 Then
            Producto = ListaProducto.Item(0)
            AsignaCampos(Producto)
        Else
            TraeConsulta(ListaProducto)
        End If

        Producto.Dispose()
        ProductoDao.Dispose()
        ProductoBusiness.Dispose()
    End Sub
    Private Sub AsignaCampos(ByVal Producto As clsCatalogo)
        txtCod.Text = Producto.Prod_Id.ToString.Trim
        txtNom.Text = Producto.Descripcion.Trim.ToUpper

        txtCod.Enabled = False
        txtNom.Enabled = False
    End Sub
    Private Sub TraeConsulta(ByVal ListaProductos As List(Of clsCatalogo))
        consultaCatalogo.ModoAcceso = "SORTEO"
        consultaCatalogo.grdCatalogo.DataSource = ListaProductos

        consultaCatalogo.grdCatalogo.Columns(0).DataPropertyName = "Prod_Id"
        consultaCatalogo.grdCatalogo.Columns(1).DataPropertyName = "Descripcion"
        consultaCatalogo.grdCatalogo.Columns(2).DataPropertyName = "Marca"
        consultaCatalogo.grdCatalogo.Columns(3).DataPropertyName = "Modelo"
        consultaCatalogo.grdCatalogo.Columns(4).DataPropertyName = "Tipo"
        consultaCatalogo.grdCatalogo.Columns(5).DataPropertyName = "Costo"
        consultaCatalogo.grdCatalogo.Columns(6).DataPropertyName = "Cantidad"
        consultaCatalogo.grdCatalogo.Columns(7).DataPropertyName = "Proveedor"
        consultaCatalogo.grdCatalogo.Columns(8).DataPropertyName = "Fecha_Ingreso"
        consultaCatalogo.grdCatalogo.Columns(9).DataPropertyName = "Cod_Suc"

        consultaCatalogo.grdCatalogo.Columns(0).HeaderText = "Codigo"
        consultaCatalogo.grdCatalogo.Columns(1).HeaderText = "Descripcion"
        consultaCatalogo.grdCatalogo.Columns(2).HeaderText = "Marca"
        consultaCatalogo.grdCatalogo.Columns(3).HeaderText = "Modelo"
        consultaCatalogo.grdCatalogo.Columns(4).HeaderText = "Tipo"
        consultaCatalogo.grdCatalogo.Columns(5).HeaderText = "Costo"
        consultaCatalogo.grdCatalogo.Columns(6).HeaderText = "Stock"
        consultaCatalogo.grdCatalogo.Columns(7).HeaderText = "Proveedor"
        consultaCatalogo.grdCatalogo.Columns(8).HeaderText = "Fecha Alta"
        consultaCatalogo.grdCatalogo.Columns(9).HeaderText = "Sucursal"

        consultaCatalogo.grdCatalogo.Columns(9).Visible = False

        consultaCatalogo.MdiParent = Me.MdiParent
        Me.Enabled = False
        consultaCatalogo.Show()
    End Sub
    Private Sub LimpiaArticulo()
        txtCod.Text = ""
        txtNom.Text = ""

        txtCod.Enabled = True
        txtNom.Enabled = True

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LimpiaArticulo()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Cliente As New clsCliente()
        Dim ClienteDao As New daoCliente()
        Dim Producto As New clsCatalogo()
        Dim ProductoDao As New daoCatalogo()
        Dim Sorteo As New clsSorteo()
        Dim sorteoDao As New daoSorteo()
        Dim Vidriera As New clsVidriera()
        Dim VidrieraDao As New daoVidriera()

        Try
            ''Valido que sea un cliente
            If txtCodClie.Text.Trim.Length = 0 Then
                MessageBox.Show("Debe ingresar un cliente", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            Cliente = ClienteDao.TraeCliente(Convert.ToInt32(txtCodClie.Text.Trim))
            If Cliente.Cli_Id = 0 Then
                MessageBox.Show("Debe ingresar un cliente valido.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

            ''Valido que sea un producto Valido
            If txtCod.Text.Trim.Length = 0 Then
                MessageBox.Show("Debe ingresar un Producto", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            Producto = ProductoDao.TraeArticulo(Convert.ToInt32(txtCod.Text.Trim))
            If Producto.Prod_Id = 0 Then
                MessageBox.Show("Debe ingresar un Producto", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

            Sorteo.Sort_Id = Numerador.TraeProximo("SORTEO")
            Sorteo.Fecha = Now.Date()
            Sorteo.Prod_Id = Producto.Prod_Id
            Sorteo.Cli_Id = Cliente.Cli_Id
            Sorteo.Estado = "P"
            Sorteo.Cod_Suc = Configuracion.Cod_Suc

            If sorteoDao.Alta(Sorteo) Then
                MessageBox.Show("El sorteo se genero correctamente con el codigo " & Sorteo.Sort_Id.ToString.Trim _
                                , "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Vidriera = VidrieraDao.TraeVidrieraProd(Producto.Prod_Id)
                Vidriera.Estado = "I"
                Vidriera.Fecha_Baja = Now.Date()
                VidrieraDao.Modificar(Vidriera)
                LimpiaArticulo()
                LimpiaCliente()
            Else
                MessageBox.Show("El sorteo no fue generado. Comunicarse con sistemas", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
        Catch ex As Exception
            Cliente.Dispose()
            ClienteDao.Dispose()
            Producto.Dispose()
            ProductoDao.Dispose()
            Sorteo.Dispose()
            sorteoDao.Dispose()
            Vidriera.Dispose()
            VidrieraDao.Dispose()

        End Try
    End Sub

    Private Sub txtCodClie_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCodClie.KeyPress
        validarInt(e)
    End Sub

    Private Sub txtDni_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDni.KeyPress
        validarInt(e)
    End Sub

    Private Sub txtNomClie_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNomClie.KeyPress
        validarString(e)
    End Sub

    Private Sub txtApeClie_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtApeClie.KeyPress
        validarString(e)
    End Sub

    Private Sub txtCod_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCod.KeyPress
        validarInt(e)
    End Sub
    Private Sub Salir()
        If txtCodClie.Text.Trim.Length > 0 Or txtCod.Text.Trim.Length > 0 Then
            Dim Respuesta As DialogResult
            Respuesta = MessageBox.Show("¿Desea salir sin guardar?", "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
            If Respuesta = Windows.Forms.DialogResult.Yes Then
                Me.Close()
            End If
        Else
            Me.Close()
        End If
    End Sub
End Class
